﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Axis.Dao.Core;
using Axis.BusinessObject.Response;
using Axis.BusinessObject.Core;
using Axis.Web.Administrator.Global;
using System.IO;
using System.Configuration;
namespace Axis.Web.Views.Accounts
{
    public partial class Verify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["Email"] != null && Request["VerifyCode"] != null)
            {
                VerifyForgotPassword(Request["Email"], Request["VerifyCode"]);
            }
            else if (Request["ID"] != null && Request["VerifyCode"] != null)
            {
                VerifyUser(Request["ID"], Request["VerifyCode"]);
            }
        }
        protected void VerifyForgotPassword(string pEmail, string pVerifyCode)
        {
            string inputValue = string.Format("<InputValue Action='RESETPASSWORD' Email='{0}' RequestCode='{1}'/>", pEmail, pVerifyCode);
            CApplicationMessage message = new CCoreDao().ActionMemberList(inputValue);
            if (message != null)
            {
                if (message.IsSuccessfull)
                {
                    string mailContent = GetContentForgotPasswordSuccessTemplate(pEmail, message.Result.ToString());
                    new CEmail().SendMail(pEmail, "[6sao.vn] Lấy lại mật khẩu thành công.", mailContent);
                }
            }
            ltrVerifyMsg.Text = message.Description;
        }
        protected void VerifyUser(string id, string verifyCode)
        {
            string inputValue = string.Format("<InputValue Action='VERIFY' ID='{0}' VerifyCode='{1}'/>", id, verifyCode);
            CCoreDao core = new CCoreDao();
            CApplicationMessage message = core.ActionMemberList(inputValue);
            if (message != null)
            {
                if (message.IsSuccessfull)
                {
                    inputValue = string.Format("<InputValue ID='{0}'/>", id);
                    IList<CMemberList> list = new CCoreDao().GetMemberList(inputValue);
                    if (list != null && list.Count > 0)
                    {
                        CMemberList member = list[0];
                        int IsVerified = member.IsVerified;
                        if (IsVerified == 0)
                        {
                            string mailContent = GetContentRegisterSuccessTemplate(member.FirstName + " " + member.LastName);
                            new CEmail().SendMail(member.Email, "[6sao.vn] Chúc mừng bạn đã đăng ký thành công.", mailContent);
                        }
                    }
                }
            }
            ltrVerifyMsg.Text = message.Description;
        }
        protected string GetContentForgotPasswordSuccessTemplate(string pEmail, string pNewPass)
        {
            try
            {
                string ret = string.Empty;
                StreamReader streamReader = File.OpenText(HttpContext.Current.Server.MapPath("~/Views/template/ForgotPassword_Success.html"));
                ret = streamReader.ReadLine();
                while (streamReader.ReadLine() != null)
                {
                    ret += streamReader.ReadLine();
                }
                streamReader.Close();
                ret = ret.Replace("[EMAIL]", pEmail);
                ret = ret.Replace("[NEW_PASS]", pNewPass);
                return ret;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
        protected string GetContentRegisterSuccessTemplate(string FullName)
        {
            try
            {
                string ret = string.Empty;
                StreamReader streamReader = File.OpenText(HttpContext.Current.Server.MapPath("~/Views/template/content_verify_success.txt"));
                ret = streamReader.ReadLine();
                while (streamReader.ReadLine() != null)
                {
                    ret += streamReader.ReadLine();
                }
                streamReader.Close();
                string domain = ConfigurationManager.AppSettings["Domain"];
                string contentMailVerifyCode = ConfigurationManager.AppSettings["ContentMailVerify"];
                ret = ret.Replace("[FULL_NAME]", FullName);
                return ret;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
    }
}